package com.powernode.shop.controller;

import com.powernode.shop.constant.LogoutConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ZHAOZHENYU
 * @since 2022/10/11
 */

@RequestMapping("/sys/logout")
@RestController
public class LogoutController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 退出登录     将token清空
     * @param request
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> logout(HttpServletRequest request){
        String header = request.getHeader(LogoutConstant.LOGOUT_AUTHORIZATION);
        String jwtToken = header.replaceFirst(LogoutConstant.LOGOUT_BEARER, "");
        redisTemplate.delete(LogoutConstant.LOGOUT_OAUTH_JWT_PREFIX+jwtToken);
        return ResponseEntity.ok().build();
    }

}
